package com.hspedu.homework;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Scanner;

/**
 * @author LiuJinTao
 * @className HomeWork01
 * @date 2024/06/23 21:37
 * @description TODO
 */
public class HomeWork01 {

    public static void main(String[] args) {
        A a1 = new A();
        B b1 = new B(a1);
        Thread a = new Thread(a1);
        Thread b = new Thread(b1);
        a.start();
        b.start();
    }

}

class A implements Runnable {
    private boolean loop = true;


    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        while (loop) {
            System.out.println((int)(Math.random()*100+1));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("a线程退出");
    }
}

class B implements Runnable {
    private A a;
    private Scanner scanner = new Scanner(System.in);
    public B(A a) {
        this.a = a;
    }

    @Override
    public void run() {
        while (true) {
            // 接收到用户输入
            System.out.println("请输入你的指令(Q)");
            char c = scanner.next().toUpperCase().charAt(0);
            if (c == 'Q') {
                a.setLoop(false);
                break;
            }
        }
        System.out.println("b线程退出");
    }

}